package com.wjzjq.hiring.company.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wjzjq.hiring.common.PageModel;
import com.wjzjq.hiring.common.util.AppUtil;
import com.wjzjq.hiring.company.dao.ApplicationDao;
import com.wjzjq.hiring.company.dao.CompanyDao;
import com.wjzjq.hiring.company.dao.PositionDao;
import com.wjzjq.hiring.company.entity.ApplicationEntity;
import com.wjzjq.hiring.company.entity.CompanyEntity;
import com.wjzjq.hiring.company.entity.PositionEntity;
import com.wjzjq.hiring.talent.dao.ResumeDao;
import com.wjzjq.hiring.talent.dao.TalentDao;
import com.wjzjq.hiring.talent.entity.ResumeEntity;
import com.wjzjq.hiring.talent.entity.TalentEntity;

/**
 * Servlet implementation class PositionServlet
 */
@WebServlet("/PositionServlet")
public class PositionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private PositionDao positionDao = new PositionDao();
	private ResumeDao resumeDao = new ResumeDao();
	private ApplicationDao applicationDao = new ApplicationDao();
	private CompanyDao companyDao = new CompanyDao();
	private TalentDao talentDao = new TalentDao();


	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置post请求的编码
		request.setCharacterEncoding("utf-8");
		// 判断逻辑
		String command = request.getParameter("command");
		if ("listAllPosition".equals(command)) {
			listAllPosition(request, response);
		} else if ("detail".equals(command)) {
			detail(request, response);
		} else if ("apply".equals(command)) {
			apply(request, response);
		} else if ("deletePosition".equals(command)) {
			deletePosition(request, response);
		} else if ("preUpdatePosition".equals(command)) {
			preUpdatePosition(request, response);
		} else if ("updatePosition".equals(command)) {
			updatePosition(request, response);
		} else if ("addPosition".equals(command)) {
			addPosition(request, response);
		} else {
			throw new RuntimeException("什么情况");
		}
	}

	private void addPosition(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		PositionEntity pe = new PositionEntity();
		pe.setCompanyId(AppUtil.getCompanyId(request, response));
		pe.setName(request.getParameter("name"));
		pe.setDuty(request.getParameter("duty"));
		pe.setAbility(request.getParameter("ability"));
		pe.setSalary1(request.getParameter("salary1") != null ? Integer.valueOf(request.getParameter("salary1")) : 0);
		pe.setSalary2(request.getParameter("salary2") != null ? Integer.valueOf(request.getParameter("salary2")) : 0);
		int row = 0;
		row = positionDao.addPosition(pe);
		if (row != 0) {
			try {
				response.sendRedirect(request.getContextPath() + "/CompanyServlet?command=main");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	private void updatePosition(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		PositionEntity pe = new PositionEntity();
		pe.setName(request.getParameter("name"));
		pe.setDuty(request.getParameter("duty"));
		pe.setAbility(request.getParameter("ability"));
		pe.setId(Integer.valueOf(request.getParameter("positionId")));
		pe.setSalary1(request.getParameter("salary1") != null ? Integer.valueOf(request.getParameter("salary1")) : 0);
		pe.setSalary2(request.getParameter("salary2") != null ? Integer.valueOf(request.getParameter("salary2")) : 0);
		int row = 0;
		row = positionDao.updatePosition(pe);
		if (row != 0) {
			try {
				response.sendRedirect(request.getContextPath() + "/CompanyServlet?command=main");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	private void listAllPosition(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<PositionEntity> positionList = positionDao.listAllPosition();
		int pageNum = 1;
		int pageSum = 6;
		String pn = request.getParameter("pageNum");
		String ps = request.getParameter("pageSum");
		if (pn != null) {
			pageNum = Integer.valueOf(pn);
		}
		if (ps != null) {
			pageSum = Integer.valueOf(ps);
		}
		PageModel<PositionEntity> pageModel = PositionDao.getAppList(positionList, pageNum, pageSum);
		if (positionList == null | pageModel == null) {
			request.getRequestDispatcher("view/common/404.jsp").forward(request, response);
		} else {
			request.setAttribute("positionList", positionList);
			request.setAttribute("pageModel", pageModel);
		}
		List<CompanyEntity> companyList = companyDao.listAllCompany();
		request.setAttribute("companyList", companyList);
		List<TalentEntity> talentList = talentDao.listAllTalent();
		request.setAttribute("talentList", talentList);
		request.getRequestDispatcher("view/company/position-list.jsp").forward(request, response);
	}

	
	private void preUpdatePosition(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		int positionId = Integer.valueOf(request.getParameter("positionId"));
		PositionEntity pe = positionDao.getPE(positionId);
		if (pe != null) {
			try {
				request.setAttribute("pe", pe);
				request.getRequestDispatcher("/view/company/position-update.jsp").forward(request, response);
			} catch (ServletException | IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void deletePosition(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		int positionId = Integer.valueOf(request.getParameter("positionId"));
		int row = 0;
		row = positionDao.deletePosition(positionId);
		if (row != 0) {
			try {
				response.sendRedirect(request.getContextPath() + "/CompanyServlet?command=main");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	private void apply(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		int positionId = Integer.valueOf(request.getParameter("positionId"));
		int resumeId = Integer.valueOf(request.getParameter("resumeId"));
		int talentId = AppUtil.getTalentId(request);
		int row = 0;
		row = applicationDao.apply(positionId, resumeId, talentId);
		try {
			if (row != 0) {
				response.sendRedirect(request.getContextPath()
						+ "/PositionServlet?addSuccess=true&command=detail&positionId=" + positionId + "&result=1");
			} else {
				response.sendRedirect(request.getContextPath()
						+ "/PositionServlet?addSuccess=false&command=detail&positionId=" + positionId + "&result=0");

			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	

	private void detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int positionId = Integer.parseInt(request.getParameter("positionId"));
		PositionEntity position = positionDao.getPE(positionId);
		if (position == null) {
			response.sendRedirect("PositionServlet?command=listAllPosition&result=" + 0);
		} else {
			// 查询所有简历
			if (AppUtil.isLogin(request)) {
				int talentId = AppUtil.getTalentId(request);
				List<ResumeEntity> resumeList = resumeDao.getResumeList(talentId);
				request.setAttribute("resumeList", resumeList);
				// 判断该职位是否已经申请
				ApplicationEntity application = applicationDao.isApply(positionId, talentId);
				request.setAttribute("application", application);
			}
			request.setAttribute("pe", position);
			request.getRequestDispatcher("view/company/position-detail.jsp").forward(request, response);
		}

	}

}
